<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

require_once('include/formbase.php');

    $feedback_focus = new edanz_Feedback();
    if(isset($_REQUEST['record'])) {
        $feedback_focus->retrieve($_REQUEST['record']);
    }

$feedback_focus = populateFromPost('', $feedback_focus);
$feedback_focus->save();

// upload attachment
if ($_FILES["attachment_c"]["error"] > 0){
	if(isset($_REQUEST["isDelete"]) && $_REQUEST["isDelete"] == "1"){
		unlink("cache/upload/".$_REQUEST["uploaded_file_name"]);
		$update_edanz_feedback_cstm = "update edanz_feedback_cstm set attachment_c = '',attachment_name_c='' where id_c ='".$feedback_focus->id."'";
		$feedback_focus->db->query($update_edanz_feedback_cstm);
	}
}else{
	if (file_exists("cache/upload/" . $_REQUEST["uploaded_file_name"])){
		unlink("cache/upload/".$_REQUEST["uploaded_file_name"]);
	}
		$attachment_file_name = create_guid();
		$update_edanz_feedback_cstm = "update edanz_feedback_cstm set attachment_c = '".$attachment_file_name."',attachment_name_c='".$_FILES["attachment_c"]["name"]."' where id_c ='".$feedback_focus->id."'";
		$feedback_focus->db->query($update_edanz_feedback_cstm);
		$_FILES["attachment_c"]["name"] = $attachment_file_name;
		move_uploaded_file($_FILES["attachment_c"]["tmp_name"],	"cache/upload/" . $_FILES["attachment_c"]["name"]);
}



    if(isset($_REQUEST['points_for_removal'])){

	$remove_point_strings = explode(",", $_REQUEST['points_for_removal']);
	foreach($remove_point_strings as $remove_point_string){
		if(!empty($remove_point_string)){
		//load relationship and add to the list
		    $feedback_focus->load_relationship('feedbackpoints');
		    $feedback_focus->feedbackpoints->delete($feedback_focus->id,$remove_point_string);
		}
	}
    }


    if(isset($_REQUEST['list_of_points'])){
        $point_strings = explode("||", $_REQUEST['list_of_points']);
        foreach($point_strings as $point_string){
            $point_values = explode("@@", $point_string);
            if(count($point_values)==4){

                if(!empty($point_values[0])){
                    //this is a selected point, as the id is already populated, retrieve and link
                    $point_focus = new edanz_FeedbackPoint();
                    $point_focus->retrieve($point_values[0]);

                    //load relationship and add to the list
                    $feedback_focus->load_relationship('feedbackpoints');
                    $feedback_focus->feedbackpoints->add($point_focus ->id);
                }else{

                    //this is a new point, as the id is not populated, need to create and link
                    $point_focus = new edanz_FeedbackPoint();
                    $point_focus->name = $point_values[1];
                    $point_focus->list_type = $point_values[2];
                    $point_focus->description = $point_values[3];
                    $point_focus->save();

                    //load relationship and add to the list
                    $feedback_focus->load_relationship('feedbackpoints');
                    $feedback_focus->feedbackpoints->add($point_focus->id);
                }

            }


        }
    }

/*
//set navigation details
$_REQUEST['return_id'] = $feedback_focus->id;
$_REQUEST['return_module'] = $feedback_focus->module_dir;
$_REQUEST['return_action'] = "FeedbackConstruction";
$_REQUEST['action'] = "DetailView";
$_REQUEST['record'] = $feedback_focus->id;;
*/
$action = "DetailView";
$module = isset($_REQUEST['return_module'])?$_REQUEST['return_module']:$feedback_focus->module_dir;
$record = isset($_REQUEST['return_id']) && !empty($_REQUEST['return_id'])?$_REQUEST['return_id']:$feedback_focus->id;

$header_URL = "Location: index.php?action=".$action."&module=".$module."&record=".$record;
 header($header_URL);

?>
